import time,unittest
from selenium import webdriver
from selenium.webdriver.common.by import By

# a3mall_test /
# ├── test_data /
# │   └── login_data.csv  # 存储登录测试数据
# ├── tests /
# │   └── test_login.py  # 测试代码
# ├── conftest.py  # pytest配置
# └── requirements.txt  # 依赖库
from ddt import ddt,data,unpack
from ke.dama import Dama
import csv
from PIL import Image


def get_login_data():
    data = []
    with open("user.csv",'r',encoding="utf-8") as file:
        requer=csv.DictReader(file)
        for i in requer:
            data.append((

                i["case_mokuai"],
                i['case_title'],
                i["case_qz"],
                i["username"],
                i["password"],


            ))
    return data

@ddt
class A3mall(unittest.TestCase):
    def setUp(self):
        print("开始")
        self.driver=webdriver.Edge()
        self.driver.maximize_window()
        self.driver.get("http://10.59.9.12:7001/")

    def tearDown(self):
        print("结束")
        self.driver.quit()
        time.sleep(2)

    @data(*get_login_data())
    @unpack

    def test_a3mall_login(self,case_mokuai, case_title, case_qz, username, password):
        print(f"{case_title}---**---{case_mokuai}---**---{case_qz}")
        # 进入登录页面
        self.driver.find_element(By.LINK_TEXT,"登录").click()
        # 断言1会员登录
        day1=self.driver.find_element(By.XPATH,'/html/body/div[4]/div/div[1]/div[1]/span[1]').text
        print("断言会员登录：",day1)
        # 输入用户名
        self.driver.find_element(By.NAME,'username').send_keys(username)
        self.driver.find_element(By.NAME,'password').send_keys(password)
        # 使用打码输入验证码
        # 截取登录成功后的图片
        self.driver.save_screenshot(r"D:\2211A\PythonProject8\cs\a3mall.png")

        # 截图出验证码
        yzm=self.driver.find_element(By.XPATH,'//*[@id="theForm"]/div[3]/span[3]/img')
        location=yzm.location
        print(location)
        x=location['x']
        y=location['y']
        size=yzm.size
        print(size)
        h=x+ size['width']
        g=y+ size['height']
        image=Image.open("a3mall.png")
        jiequyz=image.crop((x*1.5,y*1.5,h*1.5,g*1.5))
        jiequyz.save("yzm.png")

        # 打码登录
        dm=Dama("cjc2212296",'1234555uuu','	971204')
        op=open("yzm.png",'rb').read()
        jg=dm.PostPic(op,1902)
        reque=jg['pic_str']
        print("打码的结果是",reque)
        # 输入验证码
        time.sleep(2)
        self.driver.find_element(By.NAME,'code').send_keys(reque)
        time.sleep(2)
        # 点击登录进入首页
        self.driver.find_element(By.CLASS_NAME,'finish-btn').click()
        time.sleep(5)
if __name__ == '__main__':
    unittest.main()




